package com.anping.controller.user;

import com.alibaba.fastjson.JSON;
import com.anping.controller.BaseController;
import com.anping.model.User;
import com.anping.service.UserService;
import com.google.common.base.Strings;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2016/7/18.
 */

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @RequestMapping("/addUser")
    public void httpAddUser(String userName, Long userTel, String userAddress) throws Exception{
        /*if (!Strings.isNullOrEmpty(userAddress)){
            userAddress = new String(userAddress.getBytes("ISO-8859-1"),"utf-8");
        }
        if (!Strings.isNullOrEmpty(userName)){
            userName = new String(userName.getBytes("ISO-8859-1"),"utf-8");
        }*/
        User user = new User();
        user.setUserName(userName);
        user.setUserTel(userTel);
        user.setUserAddress(userAddress);
        user.setUserCreatedate(new Date());
        boolean flag = userService.addUser(user);

        response.setCharacterEncoding("UTF-8"); //设置编码格式
        response.setContentType("text/html");   //设置数据格式
        Writer writer = response.getWriter();
        try {
            writer.write(JSON.toJSONString(user));
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            writer.flush();
            writer.close();
            logger.info(JSON.toJSONString(user));
        }
    }


    @RequestMapping("/findUserById")
    public void findUserById(int userId) throws Exception{
        User user = userService.findUserById(userId);
        response.setCharacterEncoding("UTF-8"); //设置编码格式
        response.setContentType("text/html");   //设置数据格式
        Writer writer = response.getWriter();
        try {
            writer.write(JSON.toJSONString(user));
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            writer.flush();
            writer.close();
            logger.info(JSON.toJSONString(user));
        }
    }

    @RequestMapping("/findUser")
    public ModelAndView findUser(String userName, Long userTel, String userAddress, Integer pageNum, Integer pageSize) throws Exception{
        /*if (!Strings.isNullOrEmpty(userAddress)){
            userAddress = new String(userAddress.getBytes("ISO-8859-1"),"utf-8");
        }
        if (!Strings.isNullOrEmpty(userName)){
            userName = new String(userName.getBytes("ISO-8859-1"),"utf-8");
        }*/
        ModelAndView model = new ModelAndView("/user/userList");
        User user = new User();
        if (userName!=null&&!"".equals(userName)){
            user.setUserName(userName);
        }
        if (userTel!=null){
            user.setUserTel(userTel);
        }
        if (userAddress!=null&&!"".equals(userAddress)){
            user.setUserAddress(userAddress);
        }


        System.out.println(JSON.toJSONString(user));
        List<User> userList = userService.findUserList(user);
        System.out.println(JSON.toJSONString(userList));
        model.addObject("userList",userList);
        model.addObject("user",user);
        //model.addObject("pageCount",pageSize);
        //model.addObject("pageNum", pageNum);
        //model.addObject("totalCount", totalCount);
        return model;
    }

    @RequestMapping("/updateUserByConsole")
    public ModelAndView updateUserByConsole(Integer userId, String userName, Long userTel, String userAddress){
        ModelAndView modelAndView = new ModelAndView("/user/userList");
        User user = new User();
        user.setUserId(userId);
        user.setUserName(userName);
        user.setUserTel(userTel);
        user.setUserAddress(userAddress);
        boolean flag = userService.updateUser(user);
        modelAndView.addObject("flag",flag);
        return modelAndView;
    }

    @RequestMapping("/updateUserByUser")
    public void updateUserByUser(int userId, String userName, Long userTel, String userAddress, String loginDate) throws Exception{
        /*if (!Strings.isNullOrEmpty(userAddress)){
            userAddress = new String(userAddress.getBytes("ISO-8859-1"),"utf-8");
        }
        if (!Strings.isNullOrEmpty(userName)){
            userName = new String(userName.getBytes("ISO-8859-1"),"utf-8");
        }*/
        User user = new User();
        user.setUserId(userId);
        user.setUserName(userName);
        user.setUserTel(userTel);
        user.setUserAddress(userAddress);
        if(Strings.isNullOrEmpty(loginDate)){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
            Date date = dateFormat.parse(loginDate);
            user.setUserLogindate(date);
        }
        boolean flag = userService.updateUser(user);
        response.setCharacterEncoding("UTF-8"); //设置编码格式
        response.setContentType("text/html");   //设置数据格式
        Writer writer = response.getWriter();
        try {
            writer.write(JSON.toJSONString(flag));
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            writer.flush();
            writer.close();
            logger.info(JSON.toJSONString(user));
        }
    }

    @RequestMapping()
    public void callDriver(String userTel, String userName, String userAddress){

    }

}
